package com.tmzn.queue;

import com.alibaba.fastjson2.JSONObject;
import com.tmzn.service.ITDevicePlanService;
import com.tmzn.socket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskRunner {
    @Autowired
    private ITDevicePlanService itDevicePlanService;
    private Long lastLogTime;

    private Long gap = 1000 * 30L;
    @Async("myTaskAsyncPool")
    public void handlerMsg(String s) {
        try {
            s = s.substring(1,s.length()-1);
            s = s.replace("\\", "");
            checkActive(s);
            JSONObject jsonObject = JSONObject.parseObject(s);
            Integer type = jsonObject.getInteger("type");
            if (type == null) {
                type = 0;
            }

            String imei = jsonObject.getString("imei");
            if(type == 101 || type == 116 || type == 103 || type == 96 || type == 113){//
                JSONObject real_data = jsonObject.getJSONObject("real_data");
                JSONObject data = new JSONObject();
                data.put("real_data",real_data);
                data.put("type",type);
                WebSocket.sendMsg(imei,data);



                if(type == 113){
//                    itChargeRecordService.endChargeNotice(imei);
                }
            }


        }catch (Exception e){
            log.error("解析异常",e);
        }
    }

    private void checkActive(String s) {
        Long now = System.currentTimeMillis();
        if(lastLogTime == null){
            lastLogTime = now;
        }
        Long gap = now-lastLogTime;
        if(gap>this.gap){
            log.info("message:,{}", s);
            lastLogTime = now;
        }
    }
}
